package com.yt.xiao.code.generate.dal.po;

import java.util.ArrayList;
import java.util.List;

import com.yt.xiao.code.generate.util.CgUtil;

public class Table {

    public Table() {
    }

    public Table(String code, String comment, String prefix) {
        this.prefix = prefix;
        this.setCode(code);
        this.comment = comment;
    }

    /**
     * 表名称前缀
     */
    private String prefix;

    /**
     * 表名称
     */
    private String code;
    /**
     * 注释名称
     */
    private String comment;
    /**
     * 驼峰名称
     */
    private String name;
    /**
     * 下划线名称
     */
    private String lowerName;

    /**
     * 联合主键列
     */
    private List<Column> pkColumnList = new ArrayList<Column>();

    /**
     * 全部列
     */
    private List<Column> columnList;

    private Column createTimeColumn;
    private Column modifyTimeColumn;

    public String getPrefix() {
        return prefix;
    }
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
        if(this.prefix!=null && code.startsWith(prefix)){
            code = code.substring(prefix.length());
        }
        this.name = CgUtil.toCamelCase(code);
        this.lowerName = CgUtil.camelToUnderLineCase(this.name);
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLowerName() {
        return lowerName;
    }

    public void setLowerName(String lowName) {
        this.lowerName = lowName;
    }

    /*public Column getPk() {
        Column pk;
        if(pkList==null || pkList.size()<1){
            pk = null;
        }else{
            
        }
        return pk;
    }

    public void setPk(Column pk) {
        this.pk = pk;
    }*/

    /**
     * 获得主键列表
     */
    public List<Column> getPkColumnList() {
        return pkColumnList;
    }

    public void setPkColumnList(List<Column> pkList) {
        this.pkColumnList = pkList;
    }

    public void addPkColumn(Column pk) {
        if(pkColumnList==null){
            pkColumnList = new ArrayList<Column>(2);
        }
        this.pkColumnList.add(pk);
    }

    public void setPkColumn(String columnName) {
        if(columnName!=null && this.columnList!=null && this.columnList.size()>0){
            for(Column column : this.columnList){
                if(columnName.equals(column.getCode())){
                    column.setPrimaryKey(true);
                    this.pkColumnList.add(column);
                }
            }
        }
    }

    public boolean isMultiPk(){
        return pkColumnList==null?false:pkColumnList.size()>1;
    }

    /**
     * 获得全部字段列表
     */
    public List<Column> getColumnList() {
        return columnList;
    }

    public void setColumnList(List<Column> columnList) {
        this.columnList = columnList;
    }

    public void addColumn(Column column) {
        if(columnList==null){
            columnList = new ArrayList<Column>(2);
        }
        //如果是 '创建时间' 字段
        if("screate_time".equals(column.getCode())){
            this.createTimeColumn = column;
        }
        //如果是 '修改时间' 字段
        if("supdate_time".equals(column.getCode())){
            this.modifyTimeColumn = column;
        }
        this.columnList.add(column);
    }

    public Column getCreateTimeColumn() {
        return createTimeColumn;
    }

    public void setCreateTimeColumn(Column createTimeColumn) {
        this.createTimeColumn = createTimeColumn;
    }

    public Column getModifyTimeColumn() {
        return modifyTimeColumn;
    }

    public void setModifyTimeColumn(Column modifyTimeColumn) {
        this.modifyTimeColumn = modifyTimeColumn;
    }

    /**
     * 主键是否包含自增字段
     * @return
     */
    public boolean getIsPkAutoincrement(){
        boolean flag;
        if(this.pkColumnList==null || this.pkColumnList.size()<1){
            flag=false;
        }else{
            flag=false;
            for(Column col : this.pkColumnList){
                if(col.isAutoincrement()){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public Column getAutoincrementColumn(){
        Column autoICol = null;
        if(this.pkColumnList!=null && this.pkColumnList.size()>0){
            for(Column pk : this.pkColumnList){
                if(pk.isAutoincrement()){
                    autoICol = pk;
                    break;
                }
            }
        }
        return autoICol;
    }

    /**
     * 为 '编辑' 字段列表
     */
    public List<Column> getColumnForEditList() {
        ArrayList<Column> ucList = new ArrayList<Column>();
        for(Column column : this.columnList){
            if(column==this.createTimeColumn){//是 '创建时间' 字段
                ;
            }else if(column==this.modifyTimeColumn){//是 '修改时间' 字段
                ;
            }else{
                ucList.add(column);
            }
        }
        return ucList;
    }

    /**
     * 获得 '添加' 字段列表
     */
    public List<Column> getColumnForAddList() {
        ArrayList<Column> list = new ArrayList<Column>();
        for(Column column : this.columnList){
            if(column==this.createTimeColumn){//是 '创建时间' 字段
                ;
            }else if(column==this.modifyTimeColumn){//是 '修改时间' 字段
                ;
            }else{
                if(column.isAutoincrement()){
                    ;
                }else{
                    list.add(column);
                }
            }
        }
        return list;
    }

    /**
     * 获得插入列List
     * @return
     */
    public List<Column> getInsertSqlColumnList(){
        List<Column> list = new ArrayList<Column>();
        if(this.columnList!=null && this.columnList.size()>0){
            for(Column col : this.columnList){
                if(col.isAutoincrement()){
                    
                }else if(this.createTimeColumn==col){
                    
                }else if(this.modifyTimeColumn==col){
                    
                }else{
                    list.add(col);
                }
            }
        }
        return list;
    }

    /**
     * 获得不包含主键的字段列表(for update sql)
     */
    public List<Column> getUpdateSqlColumnWithoutPKList() {
        ArrayList<Column> ucList = new ArrayList<Column>();
        for(Column column : this.columnList){
            if(column==this.createTimeColumn){//是 '创建时间' 字段
                ;
            }else if(column==this.modifyTimeColumn){//是 '修改时间' 字段
                ;
            }else{
                boolean addFlag = true;//是否是普通字段(非主键,'创建时间')
                if(this.pkColumnList!=null){
                    for(Column pkc : this.pkColumnList){
                        if(pkc==column){
                            addFlag = false;
                            break;
                        }
                    }
                }
                if(addFlag){
                    ucList.add(column);
                }
            }
        }
        return ucList;
    }
}
